package cn.cc.flex
{
	
import com.adobe.serialization.json.JSON;
	public class Json2VO
	{
		public function Json2VO()
		{
		}
		
		public static function convertObject(jsonObject:Object,voClass:Class):*{
			var vo:Object = IValueObject(new voClass());
			
			if(vo!=null){ 
				vo[vo.key] = jsonObject['pk'];
				vo['model']= jsonObject['model'];
				var fields:Object = jsonObject['fields'];
				for (var item:String in fields) {
					vo[item] = fields[item];
				}
				//fields = mx.utils.ObjectUtil.getClassInfo(jsonObject['fields']);
				//mx.controls.Alert.show(fields.toString());
			}
			return vo;
		}
		public static function convertArray(jsonObjs:Array,voClass:Class):Array{
			var vos:Array = new Array();
			for each(var item:Object in jsonObjs){
				vos.push(Json2VO.convertObject(item,voClass));
			}
			return vos;
		}
		public static function convert(jsonStr:String,voClass:Class):*{
			var obj:Object = JSON.decode(jsonStr);
			if(obj is Array){
				return Json2VO.convertArray(obj as Array,voClass);
			}else{
				return Json2VO.convertObject(obj,voClass);
			}
		}
	}
}